<?php
/**
*账务部
*作者：*****
*/

namespace App\Http\Controllers\admin;

use App\model\Expenditure;
use App\model\Fine;
use App\model\Leave;
use App\model\Salary;
use Illuminate\Http\Request;
use App\model\User;
use App\model\Cost;
use App\model\Region;
use App\Http\Requests;
use DB;

class FinanceController extends Controller
{
    public function user(){
        return view('finance.user');
    }
    /**
    *个人信息
     *作者：*****
    */
    public function information(){
        $id=1;
        $user=new User();
        $arr=$user->getData($id);
        $arr=$arr->toArray();
    	return view('finance.information',array('arr'=>$arr));

    }
    /**
    *收费明细
     * 作者：*****
    */
    public function chargedetail(){
        $cost=new Cost();
        $costlist=$cost->costList();
        $costlist=$costlist->toArray();
    	return view('finance.chargedetail',array('costlist'=>$costlist));
    }
    /**
     * 收费明细页面搜索
     * 作者：*****
     */
    public function search(){
        $use_num=$_POST['use_num']?$_POST['use_num']:'';
        $use_name=$_POST['use_name']?$_POST['use_name']:'';
        $c_mold=$_POST['c_mold']?$_POST['c_mold']:'';
        $cost=new Cost();
        $costlist=$cost->costSearch($use_num,$use_name,$c_mold);
        $costlist=$costlist->toArray();
        return view('finance.chargedetail',array('costlist'=>$costlist));
    }
    /**
    *欠费明细
     * 作者：*****
    */
    public function arrearslist(){
        $cost=new Cost();
        $arrearslist=$cost->arrearsList();
        $arrearslist=$arrearslist->toArray();
        //print_r($arrearslist);die;
    	return view('finance.arrearslist',array('arrearslist'=>$arrearslist));
    }
    /**
     * 欠费搜索
     * 作者：*****
     */
    public function arrearssearch(){
        $use_num=$_POST['use_num']?$_POST['use_num']:'';
        $use_name=$_POST['use_name']?$_POST['use_name']:'';
        $c_mold=$_POST['c_mold']?$_POST['c_mold']:'';
        $arrearslist = DB::select("select * from cost INNER JOIN user on cost.use_id=`user`.use_id where (cost.c_notes='欠费' and `user`.use_name='$use_name') or (cost.c_notes='欠费' and `cost`.c_mold='$c_mold')or(cost.c_notes='欠费' and `user`.use_num='$use_num')");
        return view('finance.arrearslist',array('object'=>$arrearslist));
    }
    /**
    *工资明细
     * 作者：*****
    */
    public function salary(){
        $use=new User();
        $users=$use->getSalary();
        $user=$users->toArray();
        //print_r($users);die;
        $leave=new Leave();
        $arr=$leave->getLeave(1);
        $users=$user['data'];
        foreach($users as $key => $value){
            $arr=$leave->getLeave($value['use_id']);
            //print_r($arr);die;
            if(empty($arr)){
                $users[$key]['kouquanqin']=0;
                $users[$key]['kouqingjia']=0;
                $users[$key]['koubuzhu']=0;
                $users[$key]['days']=0;
            }elseif(date('m',time())-1==date('m',strtotime($arr['lea_start']))){
                $users[$key]['kouquanqin']=$value['wag_atten'];
                $users[$key]['koubuzhu']=$value['wag_subsidy'];
                $fine=new Fine();
                $arr=$fine->fineXiang($value['use_id']);
                if($arr){
                    $users[$key]['kouqingjia']=$arr['fin_money'];
                }else{
                    $users[$key]['kouqingjia']=0;
                }
            }else{
                $users[$key]['kouquanqin']=0;
                $users[$key]['kouqingjia']=0;
                $users[$key]['koubuzhu']=0;
                $users[$key]['days']=0;
            }
        }
        //print_r($users);die;
        $salary=new Salary();
        $states=$salary->insertSalary($users);
        $states=$states->toArray();
        //print_r($users);die;
    	return view('finance.salary',['users'=>$users,'states'=>$states,'user'=>$user]);
    }
    /**
     * 发工资
     * 作者：*****
     */
    public function fa_salary(){
        $use_id=$_GET['use_id'];
        $num=$_GET['num'];
        $user=new User();
        $arr=$user->getfaSalary($use_id);
        $phone=$arr->use_phone;
        $yanzheng='工资已发,共计：'.$num.'【元】';
        $post_data = "account=lbh15313442331&password=lbh15313442331&mobile=$phone&content=".rawurlencode("您的验证码是：".$yanzheng."。请不要把验证码泄露给其他人。如非本人操作，可不用理会！");
        $target = "http://sms.106jiekou.com/utf8/sms.aspx";
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $target);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_NOBODY, true);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
        $return_str = curl_exec($curl);
        curl_close($curl);
        if($return_str==100){
            $salary=new Salary();
            $salary->editState($use_id);
        }
        echo $return_str;
    }
    /**
    *支出明细
    */
    public function expenditure(){
        $expenditure=new Expenditure();
        $arr=$expenditure->expenditure_lis();
        $arr=$arr->toArray();
    	return view('finance.expenditure',['arr'=>$arr]);
    }
    /**
    *收入
    */
    public function income(){
        $cost=new Cost();
        $salary=new Salary();
        $expenditure=new Expenditure();
        $zongshouru=$cost->income();
        $qita=$expenditure->income();
        $zongyuangong=$salary->income();
        //echo $zongyuangong;die;
        //echo $zongshouru-$zongyuangong-$qita;die;
        $arr=array($zongshouru,$zongyuangong,$qita);
    	return view('finance.income',['arr'=>$arr]);
    }
}
